<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item :label="t('su.product.code')" prop="code">
        <el-input
          v-model="queryParams.code"
          :placeholder="t('common.inputText') + t('su.product.code')"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="type" prop="type">
        <el-select v-model="queryParams.type" placeholder="choose type" clearable class="!w-240px">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.SU_PRODUCT_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="keyword" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="please input keyword"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item :label="t('su.product.brand')" prop="brandId">
        <el-select
          v-model="queryParams.brandId"
          :placeholder="t('common.inputText') + t('su.product.brand')"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="brand in brands"
            :key="brand.id"
            :label="brand.name"
            :value="brand.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> {{ t('action.search') }}</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> {{ t('action.reset') }}</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['su:product:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> {{ t('action.create') }}
        </el-button>
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          @click="handleExport"-->
<!--          :loading="exportLoading"-->
<!--          v-hasPermi="['su:product:export']"-->
<!--        >-->
<!--          <Icon icon="ep:download" class="mr-5px" /> 导出-->
<!--        </el-button>-->
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
<!--      <el-table-column label=" 编号" align="center" prop="id" />-->
      <el-table-column :label="t('su.product.code')" align="center" prop="code" min-width="100px" fixed="left">
        <template #default="scope">
          <router-link :to="'/su/sku/info/' + scope.row.id">
            <span link type="primary">{{ scope.row.code }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="Type" align="center" prop="type" />
      <el-table-column :label="t('su.product.material')" align="center" prop="material">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SU_PRODUCT_MATERIAL" :value="scope.row.material" />
        </template>
      </el-table-column>
      <el-table-column :label="t('su.product.laser')" align="center" prop="laser">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SU_PRODUCT_LASER" :value="scope.row.laser" />
        </template>
      </el-table-column>
      <el-table-column :label="t('su.product.polish')" align="center" prop="polish">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SU_PRODUCT_POLISH" :value="scope.row.polish" />
        </template>
      </el-table-column>
      <el-table-column :label="t('su.product.dyed')" align="center" prop="dyed">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SU_PRODUCT_DYED" :value="scope.row.dyed" />
        </template>
      </el-table-column>
      <el-table-column :label="t('su.product.finishing')" min-width="100px" align="center" prop="finishing">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SU_PRODUCT_FINISHING" :value="scope.row.finishing" />
        </template>
      </el-table-column>
      <el-table-column :label="t('su.product.keyword')" align="center" prop="keyword" />
      <el-table-column :label="t('su.product.category')" align="center" prop="categoryId" />
      <el-table-column :label="t('su.product.brand')" align="center" prop="brandId" />
      <el-table-column :label="t('su.product.unit')" align="center" prop="unit">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SU_PRODUCT_UNIT" :value="scope.row.unit" />
        </template>
      </el-table-column>
      <el-table-column :label="t('form.operate')" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SU_PRODUCT_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column :label="t('form.operate')" align="center" min-width="200px" fixed="right">
        <template #default="scope">
          <router-link :to="'/su/sku/info/' + scope.row.id">
            <el-button link type="primary">{{ t('action.preview') }}</el-button>
          </router-link>
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['su:product:update']"
          >
            {{ t('action.edit') }}
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['su:product:delete']"
          >
            {{ t('action.del') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ProductForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getIntDictOptions, getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import download from '@/utils/download'
import { ProductApi, ProductVO } from '@/api/su/product'
import { CommonApi } from "@/api/su/common"
import ProductForm from './ProductForm.vue'

/** 商品 列表 */
defineOptions({ name: 'SuProduct' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<ProductVO[]>([]) // 列表的数据
const categorys = ref([])
const brands = ref([])

const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  type: undefined,
  code: undefined,
  material: undefined,
  keyword: undefined,
  laser: undefined,
  polish: undefined,
  dyed: undefined,
  finishing: undefined,
  categoryId: undefined,
  brandId: undefined,
  status: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const getCategory = async () => {
  const data = await CommonApi.getCategoryList()
  categorys.value = data
}

const getBrand = async () => {
  const data = await CommonApi.getBrandList()
  brands.value = data
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ProductApi.getProductPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ProductApi.deleteProduct(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ProductApi.exportProduct(queryParams)
    download.excel(data, '商品.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getCategory()
  getBrand()
  getList()
})
</script>
